package models;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import play.db.ebean.Model;

/*
 * CartItem model.
 * Every cart has a cart item.
 * This model holds product and a quantity
 * that customer wants to buy.
 * Author: Axakalov Sundet
 */

@Entity
public class CartItem extends Model {
	
	@Id
	public Long id;
	
	@Column(nullable = true)
	@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
	public Product product;
	public Integer quantity;
	
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
	public Cart cart;
	
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	public UserOrder userOrder;
	
	public static Finder<Long, CartItem> find = new Finder<Long, CartItem>(Long.class, CartItem.class);
	
	public static CartItem create(Product product, int quantity)
	{
		CartItem item = new CartItem();
		item.product = product;
		item.quantity = quantity;
		return item;
	}
}
